package com.t.bricks.bussiness.service.dictionary.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.t.bricks.bussiness.db.entity.dictionary.Dictionary;
import com.t.bricks.bussiness.service.dictionary.DictionaryService;
import com.t.bricks.model.MsgEmity;

/**
 * 字典表'Dictionary表'基本业务操作类接口实现
 */
@Service
public class DictionaryServiceImpl implements DictionaryService<Dictionary> {

	/**
	 * 添加数据到指定组下
	 * @param dictionary
	 * @return
	 */
	@Override
	public MsgEmity addToGroup(Dictionary dictionary) {
		if (null == dictionary) {
			return MsgEmity.err(8001, "参数对象不能为空!");
		}
		
		if (null == dictionary.getsGroupName() || "".equals(dictionary.getsGroupName().trim())) {
			return MsgEmity.err(8002, "字典组不能为空!");
		}

		Dictionary temp = new Dictionary();
		temp.setsOnlyign(dictionary.getsGroupName());
		
		MsgEmity me = this.findByKey(temp, false);
		if (!me.isSuccess()) {
			return me;
		}
		
		List<Dictionary> list = me.getData();
		if(list.size() < 1) {
			return MsgEmity.err(8002, "字典组不存在,不能用此方法添加!");
		}
		
		dictionary.setsPid(list.get(0).getsId());// 父编号就是查出来的数据
		dictionary.setsOnlyign(String.format("%s%s", dictionary.getsGroupName(), dictionary.getsValue()));
		
		if (null == dictionary.getsEnglish() || "".equals(dictionary.getsEnglish().trim())) {
			dictionary.setsEnglish(dictionary.getsOnlyign());
		}
		
		return this.add(dictionary);
	}

}
